package com.tourlaid.mamagerment.ao.pc.impl

import com.github.pagehelper.Page
import com.github.pagehelper.PageHelper
import com.tourlaid.mamagerment.ao.pc.ShopGoodsAo
import com.tourlaid.mamagerment.entity.ResponseData
import com.tourlaid.mamagerment.entity.Shop
import com.tourlaid.mamagerment.entity.ShopGoods
import com.tourlaid.mamagerment.service.GoodsService
import com.tourlaid.mamagerment.service.ShopGoodsService
import com.tourlaid.mamagerment.service.ShopService
import com.tourlaid.mamagerment.vo.ShopGoodsListVo
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import org.springframework.util.CollectionUtils
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/8/1.
 */
@Service("shopGoodsAo")
class ShopGoodsAoImpl : ShopGoodsAo {

    @Resource
    val shopGoodsService: ShopGoodsService? = null
    @Resource
    val shopService: ShopService? = null
    @Resource
    val goodsService: GoodsService? = null


    override fun list(pageNo: Int, pageSize: Int): ResponseData<ArrayList<ShopGoodsListVo>> {
        var voList = ArrayList<ShopGoodsListVo>()
        var responseData = ResponseData(voList)
        PageHelper.startPage<Shop>(pageNo, pageSize, true)
        var page = shopGoodsService!!.queryForList() as Page<ShopGoods>
        responseData.totalCount = page.getTotal()
        val shopGoodsList = page.getResult()
        if (!CollectionUtils.isEmpty(shopGoodsList)) {

            val shopIdList = shopGoodsList.map { shopGoods -> shopGoods.shopId }
            val goodsIdList = shopGoodsList.map { shopGoods -> shopGoods.goodsId }
            val shopList = shopService!!.queryByIdList(shopIdList)
            val goodsList = goodsService!!.queryByIdList(goodsIdList)
            val shopMap = shopList.associate { Pair(it.id, it.shopName) }
            val goodsMap = goodsList.associate { Pair(it.id, it.goodsName) }

            for (shopGoods in shopGoodsList) {
                var vo = ShopGoodsListVo()
                BeanUtils.copyProperties(shopGoods, vo)
                vo.shopName = shopMap.get(shopGoods.shopId)
                vo.goodsName = goodsMap.get(shopGoods.goodsId)
                voList.add(vo)
            }
        }
        return responseData
    }
}